<?php

namespace App\Http\Controllers;

use App\EcmsNew;
use App\EcmsNewClass;
use App\Http\Extend\Helper;
use App\Http\Transformers\NewsTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ApiController extends BaseController
{
    protected $file_api = ['n.id','n.titleurl','n.title','n.titlepic','n.smalltext','c.classname','n.classid'];
    protected $tuofa = [19,21,22,74,23,24,25,26,27,28,66,69,70,72,60,61,62,63,64,65,101,102,103,104,105,107,73,12,33,67,68,88,89,90,91,92,93,96,97,98,99,112,113,114,115,122,123,124,125];
    protected $hot = [141,142,143];
    protected $yangFa = [29,31,32,126];
    protected $zhiFa = [276,277,278,279,280,281,282,283,284,285];
    protected $faXing = [168,169,170,171,172,173,174,175,176,177,270];
    /**
     * 文章详情数据
     * @param string $id
     * @return \Dingo\Api\Http\Response
     * create by wenQing
     */
    public function articleInfo(Request $request)
    {
        $article = EcmsNew::findOrFail($request->id);

        return $this->response->item($article, new NewsTransformer());
    }

    /**
     * 获取数据公共方法
     * @param $classArray
     * @param string $page
     * @param string $rows
     * @param string $isGood
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function getData($classArray,$page = '',$rows = '4',$isGood = '')
    {
        $rows = $rows ? $rows : 5;
        $page = $page ? $page : 0;
        $start = ($page-1)*$rows + 1;

        $temp = DB::table('phome_ecms_news as n')->leftJoin('phome_enewsclass as c','n.classid','=','c.classid')
            ->select($this->file_api)->orderBy('n.id','desc')->offset($start)->limit($rows);
        if(is_array($classArray))
        {
            $temp = $temp->whereIn('n.classid',$classArray);
        }
        else
        {
            $temp = $temp->where('n.classid',$classArray);
        }
        if($isGood) $temp = $temp->where('n.isgood','=',$isGood);
        $data = $temp->get();

        foreach ($data as $k => $v)
        {
            if(!empty($v->titlepic) && !preg_match('/www./',$v->titlepic)) $data->$k->titlepic = 'http://www.haotoufa.com'.$v->titlepic;
        }

        return $data;
    }

    /**
     * 首页所需要的数据
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function indexData()
    {
        $banner = EcmsNew::where('isgood',9)->orderBy('id', 'desc')->limit(5)->get(['id','titleurl','title','titlepic']);

        return response()->json([
            'hotData' => $this->getData([141,142,143],1,3,''),
            'yangFaData' => [
                'banner' => $this->getData([29,31,32,126],'',1,1),
                'data' => $this->getData([29,31,32,126],1,4)
            ],
            'tuoFaData' => [
                'banner' => $this->getData($this->tuofa,'',1,1),
                'data' => $this->getData($this->tuofa,1,4)
            ],
            'zhiFaData' => [
                'banner' => $this->getData([276,277,278,279,280,281,282,283,284,285],'',1,1),
                'data' => $this->getData([276,277,278,279,280,281,282,283,284,285],1,4)
            ],
            'faXingData' => [
                'banner' => $this->getData([168,169,170,171,172,173,174,175,176,177,270],'',1,1),
                'data' => $this->getData([168,169,170,171,172,173,174,175,176,177,270],1,4)
            ],
            'menu' => config('menu'),
            'banner' => $banner,
        ]);
    }
    /**
     * 热门数据
     */
    public function hotData(Request $request)
    {
        return response()->json($this->getData($this->hot,$request->page,3,''));
    }

    /**
     * 直接返回html数据
     * @param Request $request
     * create by wenQing
     */
    public function hotDataHtml(Request $request)
    {
        return $this->transHtml($this->getData($this->hot,$request->page,3,''));
    }

    /**
     * 养发数据
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function yangFaData(Request $request)
    {
        return response()->json($this->getData([29,31,32,126],$request->page,4));
    }

    /**
     * 养发html
     * @param Request $request
     * create by wenQing
     */
    public function yangFaDataHtml(Request $request)
    {
        return $this->transHtml($this->getData($this->yangFa,$request->page,3,''));
    }

    /**
     * 脱发数据
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function tuoFaData(Request $request)
    {
        return response()->json($this->getData($this->tuofa,$request->page,4));
    }

    /**
     * 脱发html
     * @param Request $request
     * create by wenQing
     */
    public function tuoFaDataHtml(Request $request)
    {
        return $this->transHtml($this->getData($this->tuofa,$request->page,3,''));
    }

    /**
     * 植发数据
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function zhiFaData(Request $request)
    {
        return response()->json($this->getData($this->zhiFa,$request->page,4));
    }

    /**
     * 植发html
     * @param Request $request
     * create by wenQing
     */
    public function zhiFaDataHtml(Request $request)
    {
        return $this->transHtml($this->getData($this->zhiFa,$request->page,3,''));
    }

    /**
     * 发型数据
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function faXingData(Request $request)
    {
        return response()->json($this->getData($this->faXing,$request->page,4));
    }

    /**
     * 发型html
     * @param Request $request
     * create by wenQing
     */
    public function faXingDataHtml(Request $request)
    {
        return $this->transHtml($this->getData($this->faXing,$request->page,3,''));
    }

    /**
     * 栏目数据返回
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function articleList(Request $request)
    {
        $class = $request->classid;
        //判断一下类别下是否有子目录
        $newsClass = EcmsNewClass::where('classid',$request->classid)->get(['classid','sonclass','classname','classpagekey','bclassid','bname','intro'])->toArray();
        $parentClass = EcmsNewClass::where('classid',$newsClass[0]['bclassid'])->get(['classname'])->toArray();
        if($newsClass[0]['sonclass'])
        {
            $class = explode('|',$newsClass[0]['sonclass']);
            foreach($class as $k => $v)
            {
                if(empty($v)) unset($class[$k]);
            }
        }

        return response()->json([
            'data' => $this->getData($class,$request->page,8),
            'class_name' => $newsClass[0]['classname'],
            'parent_class_name' => $parentClass ? $parentClass[0]['classname'] : ''
        ]);
    }

    /**
     * 更多分类数据
     * @param Request $request
     * create by wenQing
     */
    public function more(Request $request)
    {
        $class = $request->classid;
        //判断一下类别下是否有子目录
        $newsClass = EcmsNewClass::where('classid',$request->classid)->get(['classid','sonclass','classname','classpagekey','bclassid','bname','intro'])->toArray();
        if($newsClass[0]['sonclass'])
        {
            $class = explode('|',$newsClass[0]['sonclass']);
            foreach($class as $k => $v)
            {
                if(empty($v)) unset($class[$k]);
            }
        }

        $data = $this->getData($class,$request->page,8);
        return $this->transHtml($data,True);
    }
    /**
     * 处理html
     * @param $arr
     * @param $smallText
     * create by wenQing
     */
    public function transHtml($arr,$smallText = '')
    {
        $html = '';
        foreach ($arr as $k => $v)
        {
            $append = '';
            if(empty($v['titlepic']))
            {
                $v['titlepic'] = asset('image/default.jpg');
            }
            if($smallText) $append = '<p>'.Helper::h_mb_substr($v['smalltext']).'</p>';
            $html .= '<div class="list-member">';
            $html .= '<div class="list-left"><a href="'.url('article/'.$v['id']).'"><img src="'.$v['titlepic'].'" /></a></div>';
            $html .= '<div class="list-right"><a href="'.Helper::transListUrl($v['classid']).'">'.$v['classname'].'</a>'.
                '<p><a class="c_333" href="'.url('article/'.$v['id']).'">'.mb_substr($v['title'],0,24).'</a></p>'.$append.'</div>';
            $html .= '</div>';
        }
        echo $html;
    }
}
